package com.wenbo.wenbo.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.wenbo.wenbo.dto.StoryDto;
import com.wenbo.wenbo.dto.UserInfoDto;
import com.wenbo.wenbo.dto.WeChatDto;
import com.wenbo.wenbo.entity.Story;
import com.wenbo.wenbo.entity.UserInfo;
import com.wenbo.wenbo.mapper.StoryMapper;
import com.wenbo.wenbo.mapper.UserInfoMapper;
import com.wenbo.wenbo.service.MsgService;
import com.wenbo.wenbo.service.WeChatService;
import org.apache.catalina.User;
import org.json.JSONException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author ywb
 * @date 2021年12月08日 10:03
 */
@Service
public class WeChatServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements WeChatService {


    @Value("${wechat.appId}")
    private String appId;
    @Value("${wechat.secret}")
    private String secret;

    @Override
    public String getAccessToken() {
        //第一次请求 获取access_token 和 openid
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId);
        paramMap.put("secret", secret);
        paramMap.put("grant_type", "client_credential");
        String obj=HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token",paramMap);
        log.error(obj);
        JSONObject jsonObject = JSONUtil.parseObj(obj);
        return jsonObject.get("access_token").toString();
    }

    @Override
    public void sendMessage(String accessToken,String time,String heavy,String day) {
        String openId="oL2zK5XXX6YFx3ylsGLsZTJelZ0Q";
        String template_id="7Aq4b8od_D1JNZkDmbeKH8WQutz9tYCEezqSDwpoINw";
        String wife="张意";
        String children="杨墨林(1.0)";
        String bornTime=time;
        String heavy2=heavy;
        String day2=day;
        String b="{\"touser\":\""+openId+"\",\"template_id\":\""+template_id+"\",\"lang\":\"zh_CN\",\"data\": {\"thing1\": {\"value\": \"事件1\"},\"thing2\": {\"value\": \"事件2\"},\"thing3\": {\"value\": \"事件3\"},\"thing4\": {\"value\": \"事件4\"},\"thing5\": {\"value\": \"事件5\"}}}";
        String c="{\"touser\":\""+openId+"\",\"template_id\":\""+template_id+"\",\"lang\":\"zh_CN\",\"data\": {\"thing1\": {\"value\": \""+wife+"\"},\"thing2\": {\"value\": \""+children+"\"},\"thing3\": {\"value\": \""+bornTime+"\"},\"thing4\": {\"value\": \""+heavy2+"\"},\"thing5\": {\"value\": \""+day2+"\"}}}";
        System.err.println(c);
        log.error(c);
        String o=HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+accessToken,c);
        log.error(o);


    }

    @Override
    public String getOpenId(String code) {
        //第一次请求 获取access_token 和 openid
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId);
        paramMap.put("secret", secret);
        paramMap.put("js_code", code);
        paramMap.put("grant_type", "authorization_code");
        String obj=HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session",paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(obj);
        //String session_key = (String) jsonObject.get("session_key");
        return jsonObject.get("openid").toString();
    }

    @Override
    public void saveUserInfo(UserInfoDto userInfoDto) {
        UserInfo user = new UserInfo();
        BeanUtils.copyProperties(userInfoDto,user);
        LambdaQueryWrapper<UserInfo> queryWrapper=new LambdaQueryWrapper(UserInfo.class);
        queryWrapper.eq(UserInfo::getOpenId,userInfoDto.getOpenId());
        List<UserInfo>  userInfo=baseMapper.selectList(queryWrapper);
        Date now=new Date();
        if(CollUtil.isEmpty(userInfo)){
            user.setCreateTime(now);
            user.setUpdateTime(now);
            user.insert();
        }else {
            user.setUpdateTime(now);
            user.setId(userInfo.get(0).getId());
            user.updateById();
        }
    }


}
